﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DflyingForum.Core.Data.NHHelper;
using DflyingForum.Core.Data.Domain.Entities;
using DflyingForum.Core.Data.Domain.ValueObjects;

namespace Core.Web.Code.UI.Controls
{
    public partial class DefaultBody : BaseControl
    {
        protected Repeater rptModule;//模块

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            _query.Add(new CriteriaParameter("Depth", 0, CriteriaOperator.Eq));
            _query.Add(new CriteriaParameter("Sort", CriteriaOperator.OrderByDesc));
            rptModule.DataSource = _bllModule.FindBy(_query);
            rptModule.DataBind();
        }

        protected void rptModule_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Module m = e.Item.DataItem as Module;
                Repeater rptChildModule = e.Item.FindControl("rptChildModule") as Repeater;
                _query.Clear();
                _query.Add(new CriteriaParameter("Depth", 1, CriteriaOperator.Eq));
                _query.Add(new CriteriaParameter("PID", m.ID, CriteriaOperator.Eq));
                _query.Add(new CriteriaParameter("Sort", CriteriaOperator.OrderByDesc));

                rptChildModule.DataSource = _bllModule.FindBy(_query);
                rptChildModule.DataBind();

                rptChildModule.ItemDataBound += new RepeaterItemEventHandler(rptChildModule_ItemDataBound);
            }
        }

        protected void rptChildModule_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Module m = e.Item.DataItem as Module;
                Repeater rptModuleManager = e.Item.FindControl("rptModuleManager") as Repeater;
                rptModuleManager.DataSource = _bllUser.GetModuleManager(m.ID, (int)EnumGroup.Moderator);
                rptModuleManager.DataBind();

                Posts lastPost = _bllPost.GetLastPost(m.ID);

                Literal ltrLastPost = e.Item.FindControl("ltrLastPost") as Literal;
                Literal ltrLastCreateTime = e.Item.FindControl("ltrLastCreateTime") as Literal;
                Literal ltrLastUser = e.Item.FindControl("ltrLastUser") as Literal;
                if (lastPost != null)
                {
                    ltrLastPost.Text = lastPost.Subject;
                    ltrLastCreateTime.Text = lastPost.CreateTime.ToString("yyyy-MM-dd hh:mm");
                    ltrLastUser.Text = lastPost.PubUser.NickName;
                }
                else
                {
                    ltrLastPost.Text = "目前没有帖子";
                    ltrLastCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm");
                    ltrLastUser.Text = "by Dflying";
                }
            }
        }
    }
}